package com.sduept.nwld.dataview.controller.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.apache.commons.lang.StringUtils;
import org.primefaces.PrimeFaces;
import org.primefaces.event.FileUploadEvent;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.util.ExcelUtil;

@Named
@ViewScoped
public class ExcelUploadManagerController extends AbstractController{
	private static final long serialVersionUID = 7446582210373159577L;
	private List<List<Object>> result = new ArrayList<List<Object>>();
	private int ingnoreRows;
	
	@PostConstruct
	public void init(){
		String rows = getParameter("ingnoreRows");
		if (!StringUtils.isEmpty(rows)) {
			ingnoreRows = Integer.valueOf(rows);
		}
	}
	
	public void handleFileUpload(FileUploadEvent event) {
		try {
			result = ExcelUtil.readExcel(event.getFile().getInputstream(),ingnoreRows);
		} catch (IOException e) {
			e.printStackTrace();
			addErrorMessage("文件解析失败");
		}
		returnRecordAttachments();
	}
	
	public void handleFileUploadDefine(FileUploadEvent event) {
		try {
			result = ExcelUtil.readExcelDefine(event.getFile().getInputstream(),ingnoreRows);
		} catch (IOException e) {
			e.printStackTrace();
			addErrorMessage("文件解析失败");
		}
		returnRecordAttachments();
	}
	
	public void returnRecordAttachments() {
		if (result != null) {
			PrimeFaces.current().dialog().closeDynamic(result);
		}
	}

	public List<List<Object>> getResult() {
		return result;
	}

	public void setResult(List<List<Object>> result) {
		this.result = result;
	}

	public int getIngnoreRows() {
		return ingnoreRows;
	}

	public void setIngnoreRows(int ingnoreRows) {
		this.ingnoreRows = ingnoreRows;
	}
}
